//
//  UMTool.m
//  032519-友盟统计SDK
//
//  Created by 强进冬 on 2019/3/26.
//  Copyright © 2019 强进冬. All rights reserved.
//

#import "UMTool.h"
#import <UMCommonLog/UMCommonLogHeaders.h>

@interface UMTool ()
@end


@implementation UMTool

+ (void)prepare  {
    [self prepareWithLogEnabled:NO encryptEnabled:NO crashReportEnabled:YES];
}

+ (void)prepareWithLogEnabled:(BOOL)logEnabled encryptEnabled:(BOOL)encryptEnabled crashReportEnabled:(BOOL)crashReportEnabled {
    
    // **注意： 此函数需在common sdk初始化前适用，默认是开启状态 **
    [MobClick setCrashReportEnabled:crashReportEnabled];

    
    if (logEnabled) {
        // 只有开启LogManager，才会打印日志
        [UMCommonLogManager setUpUMCommonLogManager];
    }
    [UMConfigure setLogEnabled:logEnabled];
    [UMConfigure setEncryptEnabled:encryptEnabled];
    
    [UMConfigure initWithAppkey:@"5c9995d72036577209000821" channel:@"App Store"];
}

+ (NSString *)deviceIDForIntegration {
    return [UMConfigure deviceIDForIntegration];
}

+ (void)setScenarioType:(eScenarioType)eSType {
    [MobClick setScenarioType:eSType];
}

+ (void)setPlatform:(UMSocialPlatformType)socialPlatformType {
    UMSocialManager *socialManager = [UMSocialManager defaultManager];
    switch (socialPlatformType) {
        // 新浪平台
        case UMSocialPlatformType_Sina:
            [socialManager setPlaform:socialPlatformType
                               appKey:@"3508931377"
                            appSecret:@"5f874393dca9c602abe9ed096af1f0b2"
                          redirectURL:@"http://www.baidu.com"];
            break;
        
        // 微信
        case UMSocialPlatformType_WechatSession:
            [socialManager setPlaform:socialPlatformType
                               appKey:@"wxdc1e388c3822c80b"
                            appSecret:nil
                          redirectURL:@"http://www.baidu.com"];
            break;
            
        // QQ
        case UMSocialPlatformType_QQ:
            [socialManager setPlaform:socialPlatformType
                               appKey:@"100424468"
                            appSecret:nil
                          redirectURL:@"http://www.baidu.com"];
            break;
        default:
            break;
    }
}

+ (void)getUserInfoWithPlatform:(UMSocialPlatformType)platform currentViewController:(id)currentViewController completion:(void (^)(id, NSError *))completion {
    [[UMSocialManager defaultManager] getUserInfoWithPlatform:platform currentViewController:currentViewController completion:completion];
}

+ (UMSocialMessageObject *)socialMessageObject {
    // 创建分享消息对象
    UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
    // 创建网页内容对象
    NSString* thumbURL =  @"https://mobile.umeng.com/images/pic/home/social/img-1.png";
    UMShareWebpageObject *shareObject = [UMShareWebpageObject shareObjectWithTitle:@"欢迎使用【友盟+】社会化组件U-Share"
                                                                             descr:@"欢迎使用【友盟+】社会化组件U-Share，SDK包最小，集成成本最低，助力您的产品开发、运营与推广！"
                                                                         thumImage:thumbURL];
    // 设置网页地址
    shareObject.webpageUrl = @"http://mobile.umeng.com/social";
    // 分享消息对象设置分享内容对象
    messageObject.shareObject = shareObject;
    return messageObject;
}

+ (UMSocialMessageObject *)messageWithText:(NSString *)text {
    UMSocialMessageObject *msgObject = [UMSocialMessageObject messageObject];
    msgObject.text = text;
    return msgObject;
}

+ (UMSocialMessageObject *)messageWithPicture:(id)picture pictureArray:(NSArray *)pictures text:(NSString *)text thumbImage:(id __nullable)thumbImage {
    UMSocialMessageObject *msgObject = [UMSocialMessageObject messageObject];
    UMShareImageObject *imageObject = [[UMShareImageObject alloc] init];
    imageObject.thumbImage = thumbImage;
    imageObject.shareImage = picture;
    imageObject.shareImageArray = pictures;
    msgObject.shareObject = imageObject;
    msgObject.text = text;
    return msgObject;
}

+ (UMSocialMessageObject *)messageWithWebpageUrlString:(NSString *)url title:(NSString *)title description:(NSString *)description thumImage:(id)thumImage {
    UMSocialMessageObject *message = [UMSocialMessageObject messageObject];
    UMShareWebpageObject *webpage = [UMShareWebpageObject shareObjectWithTitle:title descr:description thumImage:thumImage];
    webpage.webpageUrl = url;
    message.shareObject = webpage;
    return message;
}

+ (UMSocialMessageObject *)messageWithMusic:(NSString *)musicUrl title:(NSString *)title description:(NSString *)description thumImage:(id)thumImage {
    UMSocialMessageObject *message = [UMSocialMessageObject messageObject];
    UMShareMusicObject *music = [UMShareMusicObject shareObjectWithTitle:title descr:description thumImage:thumImage];
    music.musicUrl = musicUrl;
    message.shareObject = music;
    return message;
}

+ (UMSocialMessageObject *)messageWithVideo:(NSString *)videoUrl title:(NSString *)title description:(NSString *)description thumImage:(id)thumImage {
    UMSocialMessageObject *message = [UMSocialMessageObject messageObject];
    UMShareVideoObject *video = [UMShareVideoObject shareObjectWithTitle:title descr:description thumImage:thumImage];
    video.videoUrl = videoUrl;
    message.shareObject = message;
    return message;
}

+ (UMSocialMessageObject *)messageWithEmotion:(NSData *)data title:(NSString *)title description:(NSString *)description thumImage:(id)thumImage {
    UMSocialMessageObject *message = [UMSocialMessageObject messageObject];
    UMShareEmotionObject *emotion = [UMShareEmotionObject shareObjectWithTitle:title descr:description thumImage:thumImage];
    emotion.emotionData = data;
    message.shareObject = emotion;
    return message;
}

+ (UMSocialMessageObject *)messageWithMiniProgram:(NSDictionary *)miniProgramInfo title:(NSString *)title description:(NSString *)description thumImage:(id)thumImage {
    UMSocialMessageObject *message = [UMSocialMessageObject messageObject];
    UMShareMiniProgramObject *miniProgram = [UMShareMiniProgramObject shareObjectWithTitle:title descr:description thumImage:thumImage];
    miniProgram.webpageUrl = miniProgramInfo[@"webpageUrl"];
    miniProgram.userName = miniProgramInfo[@"userName"];
    miniProgram.path = miniProgramInfo[@"path"];
    miniProgram.hdImageData = miniProgramInfo[@"hdImageData"];
    miniProgram.miniProgramType = UShareWXMiniProgramTypeRelease;
    message.shareObject = miniProgram;
    return message;
}

+ (void)shareMenuBarConfigure {
    [UMSocialShareUIConfig shareInstance].shareTitleViewConfig.isShow = NO;
    [UMSocialShareUIConfig shareInstance].sharePageGroupViewConfig.sharePageGroupViewPostionType = UMSocialSharePageGroupViewPositionType_Middle;
    [UMSocialShareUIConfig shareInstance].sharePageScrollViewConfig.shareScrollViewPageMaxRowCountForPortraitAndMid = 2;
    [UMSocialShareUIConfig shareInstance].sharePageScrollViewConfig.shareScrollViewPageMaxColumnCountForPortraitAndMid = 3;
    [UMSocialShareUIConfig shareInstance].sharePageScrollViewConfig.shareScrollViewPageMaxRowCountForLandscapeAndMid = 2;
    [UMSocialShareUIConfig shareInstance].sharePageScrollViewConfig.shareScrollViewPageMaxColumnCountForLandscapeAndMid = 6;
    [UMSocialShareUIConfig shareInstance].shareCancelControlConfig.isShow = NO;
}

+ (void)shareMenuBarAddCustomPlatform {
    [UMSocialUIManager addCustomPlatformWithoutFilted:UMSocialPlatformType_UserDefine_Begin + 1
                                     withPlatformIcon:[UIImage imageNamed:@"music"]
                                     withPlatformName:@"copy"];
    [UMSocialShareUIConfig shareInstance].sharePageGroupViewConfig.sharePageGroupViewPostionType = UMSocialSharePageGroupViewPositionType_Bottom;
    [UMSocialShareUIConfig shareInstance].sharePageScrollViewConfig.shareScrollViewPageItemStyleType = UMSocialPlatformItemViewBackgroudType_None;
}

+ (void)showShareMenuWithDefinePlatforms:(NSArray *)platforms completion:(void (^)(UMSocialPlatformType, NSDictionary *))completion {
    [UMSocialUIManager setPreDefinePlatforms:platforms];
    [UMSocialUIManager showShareMenuViewInWindowWithPlatformSelectionBlock:^(UMSocialPlatformType platformType, NSDictionary *userInfo) {
        // 根据获取的platformType确定所选平台进行下一步操作
        !completion ?: completion(platformType, userInfo);
    }];
}


+ (void)shareToPlatform:(UMSocialPlatformType)platformType
          messageObject:(UMSocialMessageObject *)messageObject
  currentViewController:(id)currentViewController
             completion:(void (^)(id, NSError *))completion {
    
    [[UMSocialManager defaultManager] shareToPlatform:platformType messageObject:messageObject currentViewController:currentViewController completion:completion];
}


@end
